package org.mozhu.learning.algo.stack.cases.calculator;

public enum Operator {
    ADD(0) {
        @Override
        public int calculate(int a, int b) {
            return a + b;
        }
    },

    SUBTRACT(0) {
        @Override
        public int calculate(int a, int b) {
            return a - b;
        }
    },

    MULTIPLY(1) {
        @Override
        public int calculate(int a, int b) {
            return a * b;
        }
    },
    DIVIDE(1) {
        @Override
        public int calculate(int a, int b) {
            return a / b;
        }
    };

    private int priority;

    Operator(int priority) {
        this.priority = priority;
    }

    public abstract int calculate(int a, int b);

    public boolean isPriorityHighThan(Operator operator) {
        if (operator == null) return true;
        return this.priority > operator.priority;
    }

    public static Operator valueOf(char operator) {
        switch (operator) {
            case '+':
                return ADD;
            case '-':
                return SUBTRACT;
            case '*':
                return MULTIPLY;
            case '/':
                return DIVIDE;
            default:
                return null;
        }
    }

    public static boolean isOperator(char operator) {
        return operator == '+' || operator == '-' || operator == '*' || operator == '/';
    }

}
